<?php
namespace app\app\controller;
use think\Controller;
class Login extends Controller{	//登录授权
	public function _empty($name){
        die('非法访问！你的IP已被监控！');
    }

    public function test1(){
    	$app = db('app')->where('id',1)->find();
    	$data['uid'] = '1';

    	$data['app_key'] = $app['key'];
    	$data['app_secret'] = $app['secret'];

    	$data['code'] = authcode($data['uid'], 'ENCODE', 'code', 60);
    	$data['km_code'] = authcode($data['code'], 'DECODE', 'code');

    	$data['oauth_token'] = apirand(16);
    	$data['openid'] = authcode($data['km_code'].'@'.$app['id'], 'ENCODE', $data['oauth_token'], 0);
    	$data['km_openid'] = authcode($data['openid'], 'DECODE', $data['oauth_token']);
    	dump($data);
    }
    public function test2(){	//模仿微信网页授权
    	$appdata=apijieshou();
    	dump($appdata);
    	//================
    	$uid = '1';
    	echo('【假设uid=】'.$uid);
    	echo('<hr>');
    	//================
    	$app = db('app')->where('id',1)->find();
    	echo('【假设APP=】');
    	dump($app);
    	echo('<hr>');
    	//================
    	$code = authcode($uid, 'ENCODE', $app['key'], 60);
    	echo('【通过uid+appkey获取code=】'.$code);
    	echo('<hr>');
    	//================
    	$app_key = $app['key'];
    	$app_secret = $app['secret'];
    	$code = $code;

    	$km_code = authcode($code, 'DECODE', $app['key']);	//反编译临时票据
    	if(!$km_code){
    		die('code无效');
    	}
    	$userinfo = userinfobyid($km_code);
    	if(!$userinfo){
    		die('code错误');
    	}
    	$app = db('app')->where('key',$app_key)->find();
    	if($app_secret <> $app['secret']){
    		die('app_secret错误');
    	}

    	$data1['refresh_token'] = authcode($app['id'], 'ENCODE', 'S$f#ukwvKFAci6Li', 604800, 4);	//60*60*24*7
    	$data1['access_token'] = authcode($app_key, 'ENCODE', 'FHmYlb63&m#ShXCx', 7200, 8);
    	$data1['openid'] = authcode($km_code.'@'.$app['id'], 'ENCODE', $app_key, 0, 0);
    	// dump(base64_encode($data1['access_token']));
    	echo('【通过code+app_key+app_secret获取access_token+openid+refresh_token】');
    	dump($data1);
    	echo('<hr>');

    	//================
    	$access_token = $data1['access_token'];
    	$openid = $data1['openid'];
    	$app_key = authcode($access_token, 'DECODE', 'FHmYlb63&m#ShXCx','',8);
    	if(!$app_key){
    		die('access_token已过期');
    	}
    	$uid_appid = authcode($openid, 'DECODE', $app_key, 0, 0);
    	dump($uid_appid);
    	$uid_appid = explode("@", $uid_appid);
    	$uid = $uid_appid[0];

    	$userinfo = userinfobyid($uid);
    	echo('【通过access_token+openid+app_secret获取用户信息】');
    	dump($userinfo);
    	echo('<hr>');
    }

    public function test3(){	//根据标准oauth2流程
		$uid = '2';
    	echo('【假设uid=】'.$uid);
    	echo('<hr>');
    	//================
    	$app = db('app')->where('id',1)->find();
    	echo('【假设APP=】');
    	dump($app);
    	echo('<hr>');
    	//================
    	
    	
    }
}
